package com.smile.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.smile.commons.md5.MD5Util;
import com.smile.mapper.UserMapper;
import com.smile.model.domain.SmilePlusUser;
import com.smile.model.dto.request.AddShoppingRequestDTO;
import com.smile.service.ShoppingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class ShoppingServiceImpl implements ShoppingService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public Boolean addShopping(AddShoppingRequestDTO addShoppingRequestDTO) {
        SmilePlusUser smilePlusUser = new SmilePlusUser();
        BeanUtil.copyProperties(addShoppingRequestDTO, smilePlusUser);
        smilePlusUser.setCreateTime(LocalDateTime.now());
        smilePlusUser.setModifyTime(LocalDateTime.now());
        smilePlusUser.setUserName(addShoppingRequestDTO.getUserName());
        smilePlusUser.setUserId(addShoppingRequestDTO.getUserId());
        smilePlusUser.setUserType(1);
        String salt = IdWorker.getIdStr();
        smilePlusUser.setSalt(salt);
        smilePlusUser.setDeleteStatus(Boolean.FALSE);
        smilePlusUser.setPassword(MD5Util.md5WithSalt(addShoppingRequestDTO.getPassword(), salt));
        userMapper.insert(smilePlusUser);
        return Boolean.TRUE;
    }
}
